package thread.muti_thread_pro_cus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mHandler on 2018/6/2 0002.
 * Description:
 */
public class MyStack {
    private List<String> list =new ArrayList<>();

    synchronized public void push(){

        try {
            while (list.size() == 1){
                System.out.println("push操作" + Thread.currentThread().getName() + "进入等待");
                this.wait();
            }
            list.add("string" + Math.random());
            this.notifyAll();
            System.out.println("push = "+list.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public String pop(){

        String value = "";
        try {
            while (list.size() == 0){
                System.out.println("pop操作" + Thread.currentThread().getName() + "进入等待");
                this.wait();
            }
            value = "" + list.get(0);
            list.remove(0);
            this.notifyAll();
            System.out.println("pop" + list.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return value;
    }
}
